package com.fyzk.activity.lottery.shuzicai.klpk;

import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.AbsListView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.shuzicai.BallInfo;
import com.fyzk.activity.lottery.shuzicai.LotteryBaseActivity;
import com.fyzk.activity.lottery.shuzicai.SelectBallLL;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper;
import com.fyzk.activity.myview.BallPopFrameLayout;
import com.fyzk.activity.myview.BallScrollView;
import com.fyzk.activity.myview.DrawBallFrameLayout;
import com.fyzk.activity.myview.WeiZhi;
import com.fyzk.bean.PrizeHistoryItem;
import com.fyzk.betzhushu.ZhuShuFactory;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.tyjc.lottery.R;
import com.fyzk.random.RandomFactory;
import com.fyzk.utils.HelpUrlConstants;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.BottomView;
import com.fyzk.view.TopTitleView;
import com.fyzk.view.ViewCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanxs11 on 17/4/10.
 * 快乐扑克
 */

public abstract class KLPKBaseActivity extends LotteryBaseActivity implements
        onTopTitleClickListener, KLPKPopupwindow.KLPKPopuponItemListener, BottomView.onBottomClickListener,
        TouZhuHelper.BallClickListener, TouZhuHelper.BallItemClickLitener, SelectBallLL.RandomBallListener {

    /* 快乐扑克 *************** */
    public static int KLPK_LOTTERY_ID_INT = 2009;
    public static int KLPK_LOTTERY_ID_RENYI = 200901;		// 任选一单式
    public static int KLPK_LOTTERY_ID_RENER = 200902;		// 任选二单式
    public static int KLPK_LOTTERY_ID_RENSAN = 200903;	// 任选三单式
    public static int KLPK_LOTTERY_ID_RENSI = 200904;		// 任选四单式
    public static int KLPK_LOTTERY_ID_RENWU = 200905;		// 任选五单式
    public static int KLPK_LOTTERY_ID_RENLIU = 200906;	// 任选六单式
    public static int KLPK_LOTTERY_ID_RENYI_FU = 200911;	// 任选一复式
    public static int KLPK_LOTTERY_ID_RENER_FU = 200912;	// 任选二复式
    public static int KLPK_LOTTERY_ID_RENSAN_FU = 200913;	// 任选三复式
    public static int KLPK_LOTTERY_ID_RENSI_FU = 200914;	// 任选四复式
    public static int KLPK_LOTTERY_ID_RENWU_FU = 200915;	// 任选五复式
    public static int KLPK_LOTTERY_ID_RENLIU_FU = 200916;	// 任选六复式
    public static int KLPK_LOTTERY_ID_TONGHUA = 200931;	// 同花单式
    public static int KLPK_LOTTERY_ID_TONGHUA_FU = 200941;	// 同花复式
    public static int KLPK_LOTTERY_ID_TONGHUASHUN = 200932;	// 同花顺单式
    public static int KLPK_LOTTERY_ID_TONGHUASHUN_FU = 200942;// 同花顺复式
    public static int KLPK_LOTTERY_ID_SHUNZI = 200933;		// 顺子单式
    public static int KLPK_LOTTERY_ID_SHUNZI_FU = 200943;		// 顺子复式
    public static int KLPK_LOTTERY_ID_BAOZI = 200934;			// 豹子单式
    public static int KLPK_LOTTERY_ID_BAOZI_FU = 200944;		// 豹子复式
    public static int KLPK_LOTTERY_ID_DUIZI = 200935;			// 对子单式
    public static int KLPK_LOTTERY_ID_DIUZI_FU = 200945;		// 对子复式
    public static int KLPK_LOTTERY_ID_BAOXUAN = 200946;		// 包选
    public static int KLPK_LOTTERY_ID_RENER_DANTUO = 200922;	// 任选二胆拖
    public static int KLPK_LOTTERY_ID_RENSAN_DANTUO = 200923;	// 任选三胆拖
    public static int KLPK_LOTTERY_ID_RENSI_DANTUO = 200924;	// 任选四胆拖
    public static int KLPK_LOTTERY_ID_RENWU_DANTUO = 200925;	// 任选五胆拖
    public static int KLPK_LOTTERY_ID_RENLIU_DANTUO = 200926;	// 任选六胆拖


    // 顶部
    private TopTitleView topView;
    private int[] popup_itemsId = {R.string.gp_klpk_renxuan1,
            R.string.gp_klpk_renxuan2, R.string.gp_klpk_renxuan3,
            R.string.gp_klpk_renxuan4, R.string.gp_klpk_renxuan5,
            R.string.gp_klpk_renxuan6, R.string.gp_klpk_caiduizi,
            R.string.gp_klpk_caibaozi,R.string.gp_klpk_caishunzi,
            R.string.gp_klpk_caitonghua,R.string.gp_klpk_caitonghuashun,
            R.string.gp_klpk_ren2_dan,R.string.gp_klpk_ren3_dan,
            R.string.gp_klpk_ren4_dan,R.string.gp_klpk_ren5_dan,
            R.string.gp_klpk_ren6_dan};
    private String[] popup_itemsStr2 = {"奖金","奖金","奖金","奖金","奖金","奖金",
            "最高奖金","最高奖金","最高奖金","最高奖金","最高奖金","","","","",""};
    private String[] popup_itemsStr3 = {"5米","33米","116米","46米","22米","12米",
            "88米","6400米","400米","90米","2150米","","","","",""};
    private List<KLPKPopupItemBean> popup_items = new ArrayList<>();
    private KLPKPopupwindow top_popup;

    private TextView endTime_tv;    // 截止时间
    private TextView phase_tv;      // 期号

    // 中间
    private LinearLayout ll_history;
    private SelectBallLL ballLinearLayout;

    private LinearLayout ball_ll_rx;
    private DrawBillView ballView_rx;
    private TextView rx_tishi;

    private LinearLayout ball_ll_duizi;
    private DrawBillView ballView_duizi;
    private CheckBox duiziBao;
    private TextView duizi_tishi;
    private TextView duizibao_tishi;

//    private LinearLayout ball_ll_baozi;
//    private DrawBillView ballView_baozi;
//    private CheckBox baoziBao;
//
    private LinearLayout ball_ll_shunzi;
    private DrawBillView ballView_shunzi;
    private CheckBox shunziBao;
//
    private LinearLayout ball_ll_tonghua;
    private DrawBillView ballView_tonghua;
    private CheckBox tonghuaBao;
    private TextView tonghua_tishi;
    private TextView tonghuabao_tishi;

    // 胆拖
    private LinearLayout ball_ll_dan;
    private DrawBillView ballView_dan;
    private DrawBillView ballView_tuo;
    private TextView dan_tishi;

    private int ball_count_rx1 = 13;
    private int ball_count_erTong = 6;
    private List<BallInfo> balls_Line_rx1 = new ArrayList();
    private List<BallInfo> balls_Line_rx2 = new ArrayList();
    private List<BallInfo> balls_Line_rx3 = new ArrayList();
    private List<BallInfo> balls_Line_rx4 = new ArrayList();
    private List<BallInfo> balls_Line_rx5 = new ArrayList();
    private List<BallInfo> balls_Line_rx6 = new ArrayList();
    private List<BallInfo> balls_Line_duizi = new ArrayList();
    private List<BallInfo> balls_Line_baozi = new ArrayList();
    private List<BallInfo> balls_Line_shunzi = new ArrayList();
    private List<BallInfo> balls_Line_tonghua = new ArrayList();
    private List<BallInfo> balls_Line_tonghuashun = new ArrayList();
    private List<BallInfo> balls_Line_ren2_dan = new ArrayList();
    private List<BallInfo> balls_Line_ren2_tuo = new ArrayList();


    private DrawBallFrameLayout ball_linear;
    private BallScrollView ball_scroll;
    private BallPopFrameLayout moveQiu;

    private int wanFaId = KLPK_LOTTERY_ID_RENYI;

    // 底部
    private BottomView bottomView;

//    private TextView prizeNum_tv;
//    private LinearLayout prize_ll, prize_Waitting;
    private int[] prizeNum_img_ids = new int[]{R.drawable.klpk_kj_1, R.drawable
            .klpk_kj_2, R.drawable.klpk_kj_3,R.drawable.klpk_kj_4};

    private TextView shake_random_tv;   // 摇一摇机选
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ac_klpk);
        setLotteryIds();
        wanFaId = KLPK_LOTTERY_ID_RENYI;

        initView();
        ViewCallBack.getInstance().AttachCountDownObj(this);

        top_popup.setCheckItem(popupId);
        topView.setCenterText(getTopTitle() + " - "
                + popup_items.get(popupId).str1);
        // clear();
        popupClickItem(popupId);
        ballClick();
    }

    @Override
    public TopTitleView getTopView() {
        return topView;
    }

    protected String[] getMenuItems() {
        return new String[]{"开奖公告", "玩法介绍","投注查询"};
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        checkBoxInit();
    }

    /**
     * 初始化LotteryIds
     */
    protected abstract void setLotteryIds();

    private void checkBoxInit() {
        List<BallInfo> da_infos = new ArrayList<BallInfo>();
        List<BallInfo> xiao_infos = new ArrayList<BallInfo>();
        List<BallInfo> dan_infos = new ArrayList<BallInfo>();
        List<BallInfo> shuang_infos = new ArrayList<BallInfo>();
        List<BallInfo> da_dan_infos = new ArrayList<BallInfo>();
        List<BallInfo> da_shuang_infos = new ArrayList<BallInfo>();
        List<BallInfo> xiao_dan_infos = new ArrayList<BallInfo>();
        List<BallInfo> xiao_shuang_infos = new ArrayList<BallInfo>();
        List<BallInfo> infos = new ArrayList<BallInfo>();
        for (int i = 0; i < arr_infos.get(0).size(); i++) {
            BallInfo ballInfo = arr_infos.get(0).get(i);
            int num = 0;
            try {
                num = Integer.parseInt(ballInfo.getNum() + "");
            } catch (Exception e) {
                // TODO: handle exception
            }

            if (ballInfo.isSelected()) {
                infos.add(ballInfo);
            }

            if (i >= (arr_infos.get(0).size() / 2)) {
                da_infos.add(ballInfo);
                if (num % 2 != 0) {
                    da_dan_infos.add(ballInfo);
                } else {
                    da_shuang_infos.add(ballInfo);
                }
            } else {
                xiao_infos.add(ballInfo);
                if (num % 2 != 0) {
                    xiao_dan_infos.add(ballInfo);
                } else {
                    xiao_shuang_infos.add(ballInfo);
                }
            }
            if (num % 2 != 0) {
                dan_infos.add(ballInfo);
            } else {
                shuang_infos.add(ballInfo);
            }
        }
        duiziBao.setChecked(balls_Line_duizi.get(
                balls_Line_duizi.size() - 1).isSelected());
//        baoziBao.setChecked(balls_Line_baozi.get(
//                balls_Line_baozi.size() - 1).isSelected());
        shunziBao.setChecked(balls_Line_shunzi.get(
                balls_Line_shunzi.size() - 1).isSelected());
        tonghuaBao.setChecked(balls_Line_tonghua.get(
                balls_Line_tonghua.size() - 1).isSelected());
    }

    private boolean isCheckBox(List<BallInfo> infos1, List<BallInfo> infos2) {
        if (infos1.size() == infos2.size()) {
            for (int i = 0; i < infos1.size(); i++) {
                if (infos1.get(i).hashCode() != infos2.get(i).hashCode()) {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }

    protected abstract String getTopTitle();

    private void initView() {
        // 顶部
        for (int i = 0; i < popup_itemsId.length; i++) {
            KLPKPopupItemBean itemBean = new KLPKPopupItemBean();
            itemBean.str1 = getString(popup_itemsId[i]);
            itemBean.str2 = popup_itemsStr2[i];
            itemBean.str3 = popup_itemsStr3[i];
            popup_items.add(itemBean);
        }

        topView = (TopTitleView) findViewById(R.id.gp_nxk3_topTitle);
        topView.setCenterText(getTopTitle() + " - "
                + popup_items.get(0).str1);
        topView.setRightVisible(View.VISIBLE);
        topView.setRightImg(R.drawable.downmenu_icon);
        topView.setRight2Visible(View.VISIBLE);
        topView.getRightImg2().setImageDrawable(getResources().getDrawable(R.drawable.icon_refresh));
        topView.setJianTouVisible(View.VISIBLE);
        topView.setOnTitleClickListener(this);

        top_popup = new KLPKPopupwindow(this,
                AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT,
                popup_items, 11);
        top_popup.setHm_PopupOnItemListener(this);

        endTime_tv = (TextView) findViewById(R.id.klpk_endtime_tv);
        phase_tv = (TextView) findViewById(R.id.klpk_phase_tv);

        // 中间
        ll_history = (LinearLayout) findViewById(R.id.klpk_ball_history);

        ballLinearLayout = (SelectBallLL) findViewById(R.id.nxk3_ball_linearlayout);
        ballLinearLayout.setRandomBallListener(this);

        ball_ll_rx = (LinearLayout) findViewById(R.id.klpk_ll_rx1);
        ballView_rx = (DrawBillView) findViewById(R.id.klpk_ballview_rx1);
        rx_tishi = (TextView) findViewById(R.id.klpk_rx_tishi);

        ball_ll_duizi = (LinearLayout) findViewById(R.id.klpk_ll_duizi);
        ballView_duizi = (DrawBillView) findViewById(R.id.klpk_ballview_duizi);
        ballView_duizi.setDrawType(1);
        duiziBao = (CheckBox) findViewById(R.id.klpk_ballview_duizi_bao);
        duiziBao.setOnClickListener(this);
        duizi_tishi = (TextView) findViewById(R.id.klpk_duizi_tishi);
        duizibao_tishi = (TextView) findViewById(R.id.klpk_duizibao_tishi);

//        ball_ll_baozi = (LinearLayout) findViewById(R.id.klpk_ll_baozi);
//        ballView_baozi = (DrawBillView) findViewById(R.id.klpk_ballview_baozi);
//        ballView_baozi.setDrawType(2);
//        baoziBao = (CheckBox) findViewById(R.id.klpk_ballview_baozi_bao);
//        baoziBao.setOnClickListener(this);
//
        ball_ll_shunzi = (LinearLayout) findViewById(R.id.klpk_ll_shunzi);
        ballView_shunzi = (DrawBillView) findViewById(R.id.klpk_ballview_shunzi);
        ballView_shunzi.setDrawType(8);
        shunziBao = (CheckBox) findViewById(R.id.klpk_ballview_shunzi_bao);
        shunziBao.setOnClickListener(this);
//
        ball_ll_tonghua = (LinearLayout) findViewById(R.id.klpk_ll_tonghua);
        ballView_tonghua = (DrawBillView) findViewById(R.id.klpk_ballview_tonghua);
        ballView_tonghua.setDrawType(9);
        tonghuaBao = (CheckBox) findViewById(R.id.klpk_ballview_tonghua_bao);
        tonghuaBao.setOnClickListener(this);
        tonghua_tishi = (TextView) findViewById(R.id.klpk_tonghua_tishi);
        tonghuabao_tishi = (TextView) findViewById(R.id.klpk_tonghuabao_tishi);

        ball_ll_dan = (LinearLayout) findViewById(R.id.klpk_ll_dantuo);
        ballView_dan = (DrawBillView) findViewById(R.id.klpk_ballview_dan);
        ballView_tuo = (DrawBillView) findViewById(R.id.klpk_ballview_tuo);
        dan_tishi = (TextView) findViewById(R.id.klpk_dantuo_tishi);

        shake_random_tv = (TextView) findViewById(R.id.klpk_shake_random);
        shake_random_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                randomBallNum();
            }
        });

        SpannableString ss = new SpannableString("三同号通选\n开出任意一个豹子即中40米");
        ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 5,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new RelativeSizeSpan(1.0f), 0, 5,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new RelativeSizeSpan(0.5f), 5, ss.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        initArrayInfos();

        // 设置关联选球SelectView  (胆拖)
        ballView_dan.setGuanLian_view(ballView_tuo);
        ballView_tuo.setGuanLian_view(ballView_dan);

        ball_linear = (DrawBallFrameLayout) findViewById(R.id.gp_nxk3_drawball_framelayout);
        ball_scroll = (BallScrollView) findViewById(R.id.gp_nxk3_ballscroll);
        moveQiu = (BallPopFrameLayout) findViewById(R.id.gp_nxk3_fl_moveqiu);
        ball_linear.setScr(ball_scroll);
        ball_linear.setFl_moveqiu(moveQiu);
        ball_linear.setUpOrDownListener(this);

        TouZhuHelper.getInstance().setBallClickListener(this);
        TouZhuHelper.getInstance().setBallItemClickLitener(this);

        // 底部
        bottomView = (BottomView) findViewById(R.id.gp_nxk3_bottom);
        bottomView.setRightText("确定");
        bottomView.setLeftText("清除");
        bottomView.setonBottomClickListener(this);
        bottomView.setMoRenText1(0 + "注 ");
        bottomView.setMoRenText2("共" + 0 + "米");

//		endTime_tv = (TextView) findViewById(R.id.nxk3_endtime_tv);
//		endTime_qi_tv = (TextView) findViewById(R.id.nxk3_qihao_tv);

//        prizeNum_tv = (TextView) findViewById(R.id.nxk3_prizenum_tv);


    }

    @Override
    protected void initArrayInfos() {
        if (arr_infos.size() == 13) {
            balls_Line_rx1 = arr_infos.get(0);
            balls_Line_rx2 = arr_infos.get(1);
            balls_Line_rx3 = arr_infos.get(2);
            balls_Line_rx4 = arr_infos.get(3);
            balls_Line_rx5 = arr_infos.get(4);
            balls_Line_rx6 = arr_infos.get(5);
            balls_Line_duizi = arr_infos.get(6);
            balls_Line_baozi = arr_infos.get(7);
            balls_Line_shunzi = arr_infos.get(8);
            balls_Line_tonghua = arr_infos.get(9);
            balls_Line_tonghuashun = arr_infos.get(10);
            balls_Line_ren2_dan = arr_infos.get(11);
            balls_Line_ren2_tuo = arr_infos.get(12);
        } else {
            arr_infos.clear();
            initBallList(balls_Line_rx1, ball_count_rx1, 1);
            initBallList(balls_Line_rx2, ball_count_rx1, 1);
            initBallList(balls_Line_rx3, ball_count_rx1, 1);
            initBallList(balls_Line_rx4, ball_count_rx1, 1);
            initBallList(balls_Line_rx5, ball_count_rx1, 1);
            initBallList(balls_Line_rx6, ball_count_rx1, 1);
            initBallList(balls_Line_duizi, ball_count_rx1, 1);
            balls_Line_duizi.add(new BallInfo("对子包选", "对子包选"));
            initBallList(balls_Line_baozi, ball_count_rx1, 1);
            balls_Line_baozi.add(new BallInfo("豹子包选", "豹子包选"));
            initBallList(balls_Line_shunzi, 12, 1);
            balls_Line_shunzi.add(new BallInfo("顺子包选", "顺子包选"));
            initBallList(balls_Line_tonghua, 4, 1);
            balls_Line_tonghua.add(new BallInfo("同花包选", "同花包选"));
            initBallList(balls_Line_tonghuashun, 4, 1);
            balls_Line_tonghuashun.add(new BallInfo("同花顺包选", "同花顺包选"));
            initBallList(balls_Line_ren2_dan, ball_count_rx1, 1);
            initBallList(balls_Line_ren2_tuo, ball_count_rx1, 1);

            arr_infos.add(balls_Line_rx1);
            arr_infos.add(balls_Line_rx2);
            arr_infos.add(balls_Line_rx3);
            arr_infos.add(balls_Line_rx4);
            arr_infos.add(balls_Line_rx5);
            arr_infos.add(balls_Line_rx6);
            arr_infos.add(balls_Line_duizi);
            arr_infos.add(balls_Line_baozi);
            arr_infos.add(balls_Line_shunzi);
            arr_infos.add(balls_Line_tonghua);
            arr_infos.add(balls_Line_tonghuashun);
            arr_infos.add(balls_Line_ren2_dan);
            arr_infos.add(balls_Line_ren2_tuo);

        }

        ballView_rx.setBallInfoList(arr_infos.get(popupId));
        ballView_duizi.setBallInfoList(arr_infos.get(6),arr_infos.get(6).size()-1);
//        ballView_baozi.setBallInfoList(arr_infos.get(7),arr_infos.get(7).size()-1);
        ballView_shunzi.setBallInfoList(arr_infos.get(8),arr_infos.get(8).size()-1);
        ballView_tonghua.setBallInfoList(arr_infos.get(9),arr_infos.get(9).size()-1);
        ballView_dan.setBallInfoList(arr_infos.get(11));
        ballView_tuo.setBallInfoList(arr_infos.get(12));
    }

    private String[] sanTong_prizes = new String[]{"奖金240米", "奖金240米",
            "奖金240米", "奖金240米", "奖金240米", "奖金240米", "开出任意一个豹子即中40米"};

    private void initBallListSanTong(List<BallInfo> balls, String[] nums,
                                     String[] prizes) {
        balls.clear();
        for (int j = 0; j < nums.length; j++) {
            BallInfo qiu;
            qiu = new BallInfo(String.valueOf(nums[j]));
            if (prizes.length == nums.length) {
                qiu.prize = prizes[j];
            }
            balls.add(qiu);
        }
    }

    // ========== popuwindow =================
    @Override
    public void onItem(int position) {

        clear();
        popupClickItem(position);
    }

    @Override
    public void popupShow() {
        topView.setJianTouRotate_shun();

    }

    @Override
    public void popupDismiss() {
        topView.setJianTouRotate_ni();

    }

    // ========== popuwindow =================

    // ========== topTitle =================
    @Override
    public void onLeftClick() {
        onBackPressed();
    }

    @Override
    public void onCenterClick() {
        top_popup.showPopupWindow(topView);
    }

    @Override
    public void onRightClick() {
        toHelp();
    }


    // ========== bottom =================
    @Override
    public void onBottomLeftClick() {
        clear();
    }

    @Override
    public void onBottomRightClick() {
        if (wanFaId == KLPK_LOTTERY_ID_RENER_DANTUO || wanFaId == KLPK_LOTTERY_ID_RENSAN_DANTUO ||
                wanFaId == KLPK_LOTTERY_ID_RENSI_DANTUO || wanFaId == KLPK_LOTTERY_ID_RENWU_DANTUO ||
                wanFaId == KLPK_LOTTERY_ID_RENLIU_DANTUO) {
            if (getZhushu() < 2) {
                AppToast.getToast().show("胆拖至少选择2注");
                return;
            }
        }
        if (!touZhu()) {
            AppToast.getToast().show("请先选择一注再投注");
        }
    }

    // ========== bottom =================

    @Override
    public boolean touZhu() {
        long zhuShu = getZhushu();
        if (zhuShu > 0) {
            int lotteryId = wanFaId;

            if (wanFaId >= KLPK_LOTTERY_ID_RENER_DANTUO && wanFaId <= KLPK_LOTTERY_ID_RENLIU_DANTUO) {  // 胆拖
                if (zhuShu < 2) {
                    AppToast.getToast().show("胆拖至少选择2注");
                    return true;
                }
            } else {
                if (zhuShu > 1) {
                    lotteryId = lotteryId + 10; // 复式玩法id
                }
            }

            touZhu1(lotteryId, zhuShu);

            return true;
        } else {
            return false;
        }

    }

    @Override
    public void clear() {

        bottomView.setMoRenText1(0 + "注 ");
        bottomView.setMoRenText2("共" + 0 + "米");

        balls_LineClear(arr_infos);

        ballView_rx.setSelect_count(0);
        ballView_duizi.setSelect_count(0);
//        ballView_baozi.setSelect_count(0);
        ballView_shunzi.setSelect_count(0);
        ballView_dan.setSelect_count(0);
        ballView_tuo.setSelect_count(0);

        duiziBao.setChecked(false);

        ballView_rx.invalidate();
        ballView_duizi.invalidate();
//        ballView_baozi.invalidate();
        ballView_shunzi.invalidate();
        ballView_dan.invalidate();
        ballView_tuo.invalidate();


    }

    private void balls_LineClear(List<List<BallInfo>> arr) {
        for (int i = 0; i < arr.size(); i++) {
            List<BallInfo> infos = arr.get(i);
            for (BallInfo ballInfo : infos) {
                ballInfo.setSelected(false);
            }
        }
    }

    @Override
    public int getLotteryId() {
        return KLPK_LOTTERY_ID_INT;
    }

    @Override
    protected void popupClickItem(int arg2) {
        popupId = arg2;
        top_popup.setCheckItem(popupId);
        topView.setCenterText(getTopTitle() + " - "
                + popup_items.get(popupId).str1);
        isYao = true;
        ball_ll_rx.setVisibility(View.GONE);
        ball_ll_duizi.setVisibility(View.GONE);
//        ball_ll_baozi.setVisibility(View.GONE);
        ball_ll_shunzi.setVisibility(View.GONE);
        ball_ll_tonghua.setVisibility(View.GONE);
        ball_ll_dan.setVisibility(View.GONE);

        if (arg2 >= 0 && arg2 <= 5) {
            ballView_rx.setBallInfoList(arr_infos.get(arg2));
        } else if (arg2 >= 6 && arg2 <= 7) {
            ballView_duizi.setDrawType(arg2);
            ballView_duizi.setBallInfoList(arr_infos.get(arg2),arr_infos.get(arg2).size()-1);
        } else if (arg2 >= 9 && arg2 <= 10) {
            ballView_tonghua.setDrawType(arg2);
            ballView_tonghua.setBallInfoList(arr_infos.get(arg2),arr_infos.get(arg2).size()-1);
        }



        switch (arg2) {
            case 0:// he
                wanFaId = KLPK_LOTTERY_ID_RENYI;
                ball_ll_rx.setVisibility(View.VISIBLE);
                rx_tishi.setText(getString(R.string.gp_klpk_rx1_tishi));
                break;
            case 1:// he
                wanFaId = KLPK_LOTTERY_ID_RENER;
                ball_ll_rx.setVisibility(View.VISIBLE);
                rx_tishi.setText(getString(R.string.gp_klpk_rx2_tishi));
                break;
            case 2:// he
                wanFaId = KLPK_LOTTERY_ID_RENSAN;
                ball_ll_rx.setVisibility(View.VISIBLE);
                rx_tishi.setText(getString(R.string.gp_klpk_rx3_tishi));
                break;
            case 3:// he
                wanFaId = KLPK_LOTTERY_ID_RENSI;
                ball_ll_rx.setVisibility(View.VISIBLE);
                rx_tishi.setText(getString(R.string.gp_klpk_rx4_tishi));
                break;
            case 4:// he
                wanFaId = KLPK_LOTTERY_ID_RENWU;
                ball_ll_rx.setVisibility(View.VISIBLE);
                rx_tishi.setText(getString(R.string.gp_klpk_rx5_tishi));
                break;
            case 5:// he
                wanFaId = KLPK_LOTTERY_ID_RENLIU;
                ball_ll_rx.setVisibility(View.VISIBLE);
                rx_tishi.setText(getString(R.string.gp_klpk_rx6_tishi));
                break;
            case 6:// he
                wanFaId = KLPK_LOTTERY_ID_DUIZI;
                ball_ll_duizi.setVisibility(View.VISIBLE);
                duizi_tishi.setText(getString(R.string.gp_klpk_duizi_tishi));
                duizibao_tishi.setText(getString(R.string.gp_klpk_duizibao_tishi));
                duiziBao.setText("对子包选");
                break;
            case 7:// he
                wanFaId = KLPK_LOTTERY_ID_BAOZI;
                ball_ll_duizi.setVisibility(View.VISIBLE);
                duizi_tishi.setText(getString(R.string.gp_klpk_baozi_tishi));
                duizibao_tishi.setText(getString(R.string.gp_klpk_baozibao_tishi));
                duiziBao.setText("豹子包选");
                break;
            case 8:// he
                wanFaId = KLPK_LOTTERY_ID_SHUNZI;
                ball_ll_shunzi.setVisibility(View.VISIBLE);
                break;
            case 9:// he
                wanFaId = KLPK_LOTTERY_ID_TONGHUA;
                ball_ll_tonghua.setVisibility(View.VISIBLE);
                tonghua_tishi.setText(getString(R.string.gp_klpk_tonghua_tishi));
                tonghuabao_tishi.setText(getString(R.string.gp_klpk_tonghuabao_tishi));
                tonghuaBao.setText("同花包选");
                break;
            case 10:// he
                wanFaId = KLPK_LOTTERY_ID_TONGHUASHUN;
                ball_ll_tonghua.setVisibility(View.VISIBLE);
                tonghua_tishi.setText(getString(R.string.gp_klpk_tonghuashun_tishi));
                tonghuabao_tishi.setText(getString(R.string.gp_klpk_tonghuashunbao_tishi));
                tonghuaBao.setText("同花顺包选");
                break;
            case 11:
                wanFaId = KLPK_LOTTERY_ID_RENER_DANTUO;
                dan_tishi.setText(getString(R.string.gp_klpk_ren2_dan_tishi));
                ballView_dan.setSelect_max(1);// 胆码最多可选球数
                ballView_tuo.setSelect_max(ball_count_rx1 - 1);// 拖码最多可选球数
                ball_ll_dan.setVisibility(View.VISIBLE);
                break;
            case 12:
                wanFaId = KLPK_LOTTERY_ID_RENSAN_DANTUO;
                dan_tishi.setText(getString(R.string.gp_klpk_ren3_dan_tishi));
                ballView_dan.setSelect_max(2);// 胆码最多可选球数
                ballView_tuo.setSelect_max(ball_count_rx1 - 1);// 拖码最多可选球数
                ball_ll_dan.setVisibility(View.VISIBLE);
                break;
            case 13:
                wanFaId = KLPK_LOTTERY_ID_RENSI_DANTUO;
                dan_tishi.setText(getString(R.string.gp_klpk_ren4_dan_tishi));
                ballView_dan.setSelect_max(3);// 胆码最多可选球数
                ballView_tuo.setSelect_max(ball_count_rx1 - 1);// 拖码最多可选球数
                ball_ll_dan.setVisibility(View.VISIBLE);
                break;
            case 14:
                wanFaId = KLPK_LOTTERY_ID_RENWU_DANTUO;
                dan_tishi.setText(getString(R.string.gp_klpk_ren5_dan_tishi));
                ballView_dan.setSelect_max(4);// 胆码最多可选球数
                ballView_tuo.setSelect_max(ball_count_rx1 - 1);// 拖码最多可选球数
                ball_ll_dan.setVisibility(View.VISIBLE);
                break;
            case 15:
                wanFaId = KLPK_LOTTERY_ID_RENLIU_DANTUO;
                dan_tishi.setText(getString(R.string.gp_klpk_ren6_dan_tishi));
                ballView_dan.setSelect_max(5);// 胆码最多可选球数
                ballView_tuo.setSelect_max(ball_count_rx1 - 1);// 拖码最多可选球数
                ball_ll_dan.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
        addMissValue();

    }

    @Override
    protected void addHistoryView(ArrayList<PrizeHistoryItem> historyItems, boolean isWaitting) {
        ll_history.removeAllViews();
        KLPKHistoryPrizeView view = new KLPKHistoryPrizeView(getContext(), historyItems, isWaitting);
        view.setBg_scrollVisibility(View.GONE);
        ll_history.addView(view.getView());
    }

    private Drawable getIcon(int code) {
        Drawable drawable= getResources().getDrawable(prizeNum_img_ids[code/100-1]);
        /// 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        return drawable;

    }

    private String getPrizeNum(int code) {
        String num = ""+code;
        if (code == 1) {
            num = "A";
        }
        if (code == 11) {
            num = "J";
        }
        if (code == 12) {
            num = "Q";
        }
        if (code == 13) {
            num = "K";
        }
        return num;
    }

    @Override
    public void randomBallNum() {
        // TODO Auto-generated method stub
        switch (wanFaId) {
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENER_DANTUO:
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENSAN_DANTUO:
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENSI_DANTUO:
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENWU_DANTUO:
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENLIU_DANTUO:
                AppToast.getToast().show("胆拖不支持机选");
                return;
            default:
                break;
        }
        clear();
        switch (wanFaId) {
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENYI:
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENYI_FU:
                wanFaId = LotteryIDUtils.KLPK_LOTTERY_ID_RENYI;
                randomRen(1, ball_count_rx1, 0);
                break;
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENER:
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENER_FU:
                wanFaId = LotteryIDUtils.KLPK_LOTTERY_ID_RENER;
                randomRen(2, ball_count_rx1, 1);
                break;
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENSAN:
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENSAN_FU:
                wanFaId = LotteryIDUtils.KLPK_LOTTERY_ID_RENSAN;
                randomRen(3, ball_count_rx1, 2);
                break;
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENSI:
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENSI_FU:
                wanFaId = LotteryIDUtils.KLPK_LOTTERY_ID_RENSI;
                randomRen(4, ball_count_rx1, 3);
                break;
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENWU:
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENWU_FU:
                wanFaId = LotteryIDUtils.KLPK_LOTTERY_ID_RENWU;
                randomRen(5, ball_count_rx1, 4);
                break;
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENLIU:
            case LotteryIDUtils.KLPK_LOTTERY_ID_RENLIU_FU:
                wanFaId = LotteryIDUtils.KLPK_LOTTERY_ID_RENLIU;
                randomRen(6, ball_count_rx1, 5);
                break;
            case LotteryIDUtils.KLPK_LOTTERY_ID_DUIZI:
            case LotteryIDUtils.KLPK_LOTTERY_ID_DIUZI_FU:
                wanFaId = LotteryIDUtils.KLPK_LOTTERY_ID_DUIZI;
                randomRen(1, ball_count_rx1, 6);
                break;
            case LotteryIDUtils.KLPK_LOTTERY_ID_BAOZI:
            case LotteryIDUtils.KLPK_LOTTERY_ID_BAOZI_FU:
                wanFaId = LotteryIDUtils.KLPK_LOTTERY_ID_BAOZI;
                randomRen(1, ball_count_rx1, 7);
                break;
            case LotteryIDUtils.KLPK_LOTTERY_ID_SHUNZI:
            case LotteryIDUtils.KLPK_LOTTERY_ID_SHUNZI_FU:
                wanFaId = LotteryIDUtils.KLPK_LOTTERY_ID_SHUNZI;
                randomRen(1, 12, 8);
                break;
            case LotteryIDUtils.KLPK_LOTTERY_ID_TONGHUA:
            case LotteryIDUtils.KLPK_LOTTERY_ID_TONGHUA_FU:
                wanFaId = LotteryIDUtils.KLPK_LOTTERY_ID_TONGHUA;
                randomRen(1, 4, 9);
                break;
            case LotteryIDUtils.KLPK_LOTTERY_ID_TONGHUASHUN:
            case LotteryIDUtils.KLPK_LOTTERY_ID_TONGHUASHUN_FU:
                wanFaId = LotteryIDUtils.KLPK_LOTTERY_ID_TONGHUASHUN;
                randomRen(1, 4, 10);
                break;
            default:
                break;
        }

        ballView_rx.invalidate();
        ballView_duizi.invalidate();
        ballView_shunzi.invalidate();
        ballView_tonghua.invalidate();
//        ballView_dan.invalidate();
//        ballView_first.invalidate();
//        ballView_two.invalidate();
//        ballView_three.invalidate();

        ballClick();

    }

    /**
     * 机选号（一排球）
     * @param num
     */
    private void randomRen(int num, int maxNum, int indext){
        int arr[] = RandomFactory.getRandom(
                LotteryIDUtils.KLPK_LOTTERY_ID_INT).randomDanHang(num, maxNum);
        for (int i = 0; i < arr.length; i++) {
            arr_infos.get(indext).get(arr[i]).setSelected(true);
        }
    }

//    /**
//     * 机选号（多排球）
//     * @param num
//     */
//    private void randomZhi(int num){
//        int arr[] = RandomFactory.getRandom(
//                LotteryIDUtils.GD11XUAN5_LOTTERY_ID_INT).randomDanHang(3);
//        int first = arr[0];
//        int two = arr[1];
//        int thire = arr[2];
//        balls_firstLine.get(first).setSelected(true);
//        balls_twoLine.get(two).setSelected(true);
//        if (num == 3) {
//            balls_threeLine.get(thire).setSelected(true);
//        }
//    }

    private int[] setEndPosition(BallInfo ballInfo) {
        int[] endX_Y = new int[2];
        ballInfo.isChecked = true;
        int left = ballInfo.rect_left;
        int right = ballInfo.rect_right;
        int top = ballInfo.rect_top;
        int bottom = ballInfo.rect_bottom;
        endX_Y[0] = left + (right - left) / 2;
        endX_Y[1] = top + (bottom - top) / 2;
        return endX_Y;
    }

    @Override
    public String getHelpUrl() {
        // TODO Auto-generated method stub
        return HelpUrlConstants.Help_KLPK;
    }

    @Override
    public String getHelpTitle() {
        // TODO Auto-generated method stub
        return getString(R.string.help_klpk);
    }

    @Override
    public void ballClick(WeiZhi info) {
        // TODO Auto-generated method stub
        WeiZhi w = info;
        // TODO Auto-generated method stub
        int left = w.getLeft() - 5;
        int right = w.getRight() + 5;
        int top = (int) ((ball_linear.getY1() - w.getPointY() + w.getTop())
                - (w.getBottom() - w.getTop()) - 10);
        int bottom = (int) (ball_linear.getY1() - w.getPointY() + w.getTop());
        Rect rect = new Rect(left, top, right, bottom);
        moveQiu.setRect(rect, w.getNum(), w.getType(), w.isShowMoveBall);
    }

    @Override
    public void ballClick() {
        // TODO Auto-generated method stub
        checkBoxInit();
        long zhushu = getZhushu();
        long money = zhushu * 2;

        bottomView.setMoRenText1(zhushu + "注 ");
        bottomView.setMoRenText2("共" + money + "米");

    }

    private long getZhushu() {
        LogUtil.d("玩法："+wanFaId);
        try {
            return ZhuShuFactory.getZhuShu(wanFaId).betZhuShu(arr_infos);
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        super.onClick(v);

        switch (v.getId()) {
            case R.id.klpk_ballview_duizi_bao:
                balls_Line_duizi.get(balls_Line_duizi.size() - 1).setSelected(
                        duiziBao.isChecked());
                balls_Line_baozi.get(balls_Line_baozi.size() - 1).setSelected(
                        duiziBao.isChecked());
                break;
            case R.id.klpk_ballview_shunzi_bao:
                balls_Line_shunzi.get(balls_Line_shunzi.size() - 1).setSelected(
                        shunziBao.isChecked());
                break;
            case R.id.klpk_ballview_tonghua_bao:
                balls_Line_tonghua.get(balls_Line_tonghua.size() - 1).setSelected(
                        tonghuaBao.isChecked());
                balls_Line_tonghuashun.get(balls_Line_tonghuashun.size() - 1).setSelected(
                        tonghuaBao.isChecked());
                break;
        }

        ballClick();
    }

    @Override
    public void random() {
        // TODO Auto-generated method stub
//		ballView_rx.invalidate();
//        isYao = true;
//        for (int i = 0; i < arr_infos.size(); i++) {
//            List<BallInfo> ballInfos = arr_infos.get(i);
//            for (int j = 0; j < ballInfos.size(); j++) {
//                if (ballInfos.get(j).isSel) {
//                    ballInfos.get(j).setSelected(true);
//                }
//                ballInfos.get(j).isSel = false;
//            }
//        }
//
//
//        ballView_rx.invalidate();
//        ballView_duizi.invalidate();
////        ballView_baozi.invalidate();
//        ballView_shunzi.invalidate();
//
//
//        ballClick();
    }

    @Override
    protected boolean setEndAndPhaseText() {
        String endTimeStr = ViewCallBack.getInstance().refreshTimeLeft(
                AppLotteryInfos.getEndTime(getLotteryId()));
        String qi = AppLotteryInfos.getPeriod(getLotteryId());
        try {
            qi = "距" + qi.substring(qi.length() - 2, qi.length()) + "期 截止:";
        } catch (Exception e) {
            // TODO: handle exception
            qi = "距--期 截止:";

        }
        phase_tv.setText(qi);
        endTime_tv.setText(endTimeStr);
        return true;
    }

    @Override
    protected void addMissValue() {
        super.addMissValue();
        if (missInfo != null) {
            switch (popupId) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    ballView_rx.setMissValues(missInfo.missValue("2009MV_RX").get(0));
                    break;
                case 6:
                    ballView_duizi.setMissValues(missInfo.missValue("2009MV_DZ").get(0));
                    break;
                case 7:
                    ballView_duizi.setMissValues(missInfo.missValue("2009MV_BZ").get(0));
                    break;
                case 8:
                    ballView_shunzi.setMissValues(missInfo.missValue("2009MV_SZ").get(0));
                    break;
                case 9:
                    ballView_tonghua.setMissValues(missInfo.missValue("2009MV_TH").get(0));
                    break;
                case 10:
                    ballView_tonghua.setMissValues(missInfo.missValue("2009MV_HS").get(0));
                    break;
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                    ballView_dan.setMissValues(missInfo.missValue("2009MV_RX").get(0));
                    ballView_tuo.setMissValues(missInfo.missValue("2009MV_RX").get(0));
                    break;

            }
        } else {
            ballView_rx.setMissValues(new ArrayList<Integer>());
            ballView_duizi.setMissValues(new ArrayList<Integer>());
            ballView_shunzi.setMissValues(new ArrayList<Integer>());
            ballView_tonghua.setMissValues(new ArrayList<Integer>());
            ballView_dan.setMissValues(new ArrayList<Integer>());
            ballView_tuo.setMissValues(new ArrayList<Integer>());
        }

    }

    @Override
    protected void net() {
        topView.startRightAnim();
        super.net();
    }

//    @Override
//    public Intent getTrendIntent() {
//        Intent intent = new Intent(getContext(), KLPKTrendActivity.class);
//        intent.putExtra(IntentParamUtils.LOTTERYID, getLotteryId());
//        return intent;
//    }
}
